package huawei.js2025;

import sort.MergeSort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * 冠亚军排名
 */
public class Question26 {


    public static class CountyMedal implements Comparable<CountyMedal> {
        private String name;
        private Integer gold;
        private Integer silver;
        private Integer bronze;

        public CountyMedal(String name, Integer gold, Integer silver, Integer bronze){
            this.name = name;
            this.gold = gold;
            this.silver = silver;
            this.bronze = bronze;
        }

        @Override
        public int compareTo(CountyMedal o) {
            if (!this.gold.equals(o.gold)) {
                return o.gold - this.gold;
            } else if (!this.silver.equals(o.silver)) {
                return o.silver - this.silver;
            } else if (!this.bronze.equals(o.bronze)) {
                return o.bronze - this.bronze;
            } else {
                return this.name.compareTo(o.name);
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();

        List<CountyMedal> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String medalInfo = sc.nextLine();
            String[] medalInfos = medalInfo.split(" ");
            CountyMedal medal = new CountyMedal(medalInfos[0], Integer.parseInt(medalInfos[1]), Integer.parseInt(medalInfos[2]), Integer.parseInt(medalInfos[3]));
            list.add(medal);
        }

        Collections.sort(list);

        for (CountyMedal medal : list) {
            System.out.println(medal.name);
        }
    }
}
